package day26.edu;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

// Created: 26/09/2025, ChangJiang Ru
public class CollectionsTest {
    @Test
    public void test1()
    {
        List list = Arrays.asList(12, 34, 45, 32, 43, 21, 67, 54, 43, 32);
        System.out.println(list);
        System.out.println("===============");

        // reversed()
        /*
        List list1 = list.reversed();
        System.out.println(list1);

         */

        // shuffle(list)
//        Collections.shuffle(list);

        // sort(list)
//        Collections.sort(list);
//        System.out.println(list);

        // sort(list, Comparator)
        Collections.sort(list, new Comparator(){
            @Override
            public int compare(Object o1, Object o2)
            {
                if(o1 instanceof Integer && o2 instanceof Integer)
                {
                    Integer i1 = (Integer)o1;
                    Integer i2 = (Integer)o2;

                    return i1.intValue() - i2.intValue();
                }
                throw new RuntimeException("Type match error");
            }
        });

        System.out.println(list);
        System.out.println("===========");

        // sort(list, Comparator)
        Collections.sort(list, new Comparator(){
            @Override
            public int compare(Object o1, Object o2)
            {
                if(o1 instanceof Integer && o2 instanceof Integer)
                {
                    Integer i1 = (Integer)o1;
                    Integer i2 = (Integer)o2;

                    return -(i1.intValue() - i2.intValue());
                }
                throw new RuntimeException("Type match error");
            }
        });
        System.out.println(list);
        System.out.println("==========");

        // Collections.swap()
        Collections.swap(list, 0, 1);
        System.out.println(list);
    }

    @Test
    public void test2()
    {
        List list = Arrays.asList(1,3,5,6,9,34,32,21,65,76,32,98,89);
        System.out.println(list);

        Object max = Collections.max(list);
        System.out.println(max);
    }
}
